import requests
from urllib import parse
import json
import re

# http://www.baidu.com/{{project_id}}/{{host_id}}/api_id
# {'project_id':1,'host_id':2}
def _replace_argument(target_str,arguments=None):
    if not arguments:
        return target_str
    while True:
        search_result = re.search(r"{{(.+?)}}",target_str)
        if not search_result:
            break
        argument_name = search_result.group(1)
        if argument_name in arguments:
            target_str = re.sub("{{"+argument_name+"}}",arguments[argument_name],target_str)
        else:
            target_str = re.sub("{{" + argument_name + "}}", argument_name, target_str)
    return target_str


def request(api,arguments=None):
    host = api.host.host
    print('host')
    print(host)
    method = api.http_method
    path = api.path
    # host:http://www.baidu.com/
    # path: /q?wd=python
    # http://www.baidu.com/q?wd=python
    url = parse.urljoin(host, path)
    url = _replace_argument(url,arguments)
    data = {}
    if len(api.data.strip())!=2:
        print("api_data..")
        print(len(api.data.strip()))
        print(type(api.data))
        data_list = json.loads(api.data, encoding='utf-8')
        print(data_list)
        for data_dict in data_list:
            # [{name:"username",value:"zhiliao"}]
            data[data_dict['name']] = _replace_argument(data_dict['value'],arguments)
        headers = {}
        if api.headers:
            header_list = json.loads(api.headers, encoding='utf-8')
            for header_dict in header_list:
                headers[header_dict['name']] = _replace_argument(header_dict['value'], arguments)
        resp = requests.request(method, url, headers=headers, data=data)
        return resp

    if api.jsonValue:
        print("api_jsonValue"+api.jsonValue)
        print(type(api.jsonValue))
        data=json.loads(api.jsonValue, encoding='utf-8')
        headers = {}
        if api.headers:
            header_list = json.loads(api.headers, encoding='utf-8')
            for header_dict in header_list:
                headers[header_dict['name']] = _replace_argument(header_dict['value'], arguments)
        resp = requests.request(method, url, headers=headers, data=json.dumps(data))
        return resp




